package com.ease.framework.layers {

   public class GridLayer extends Layer {
      protected var gridX:Number = 20;
      protected var gridY:Number = 20;

      protected var oldW:Number = 0;
      protected var oldH:Number = 0;

      public function GridLayer() {
         super();
         oldW = this.width;
         oldH = this.height;
      }

      override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
         super.updateDisplayList(unscaledWidth, unscaledHeight);

         if (this.visible) {
            if (oldW != this.width || oldH != this.height) {
               oldW = this.width;
               oldH = this.height;
               graphics.clear();
               paintGrid();
            }
         }
      }

      protected function paintGrid(clear:Boolean = true):void {

         var x:Number = 0;
         var i:int = 0;
         while (x <= this.width) {

            if (i % 2 == 0) {
               graphics.lineStyle(1, 0xC6D4C7, 0.8, true);
            }
            else {
               graphics.lineStyle(1, 0xCCE8CF, 0.3, true);
            }
            i++;

            graphics.moveTo(x, 0);
            graphics.lineTo(x, this.height);
            x = x + gridX;
         }
         var y:Number = 0;
         i = 0;
         while (y <= this.height) {

            if (i % 2 == 0) {
               graphics.lineStyle(1, 0xC6D4C7, 0.6, true);
            }
            else {
               graphics.lineStyle(1, 0xCCE8CF, 0.5, true);
            }
            i++;

            graphics.moveTo(0, y);
            graphics.lineTo(this.width, y);
            y = y + gridX;
         }
      }
   }
}